//
//  VisitorView.swift
//  weibo
//
//  Created by William Peng on 16/3/21.
//  Copyright © 2016年 William Peng. All rights reserved.
//

import UIKit

class VisitorView: UIView {

    ///required必须执行这个方法
    ///
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        prepareUI()

    }
    
    ///UI界面
    
    private func prepareUI(){
        ///背景色是白灰色只调white值
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        
        //添加子控件
        addSubview(iconView)
        addSubview(ZheZhaoView)
        addSubview(houseView)
        addSubview(textLabel)
        addSubview(ZhuCeButton)
        addSubview(LoginButton)
        
        //添加约束
        //关闭自动布局**不要将Autoresizing转成AutoLayout的约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
        ZheZhaoView.translatesAutoresizingMaskIntoConstraints = false
        houseView.translatesAutoresizingMaskIntoConstraints = false
        textLabel.translatesAutoresizingMaskIntoConstraints = false
        ZhuCeButton.translatesAutoresizingMaskIntoConstraints = false
        LoginButton.translatesAutoresizingMaskIntoConstraints = false

        /*
        1.item: 要约束的View
        2.attribute: 要约束的view的属性
        3.relatedBy: 参照
        4.toItem: 参照的View
        第五个参数:指定view2的属性attr2
        第六个参数:指定一个与view2属性相乘的乘数multiplier
        第七个参数:指定一个与view2属性相加的浮点数constant
        */
        ///转轮动画
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        //屏幕遮罩
        addConstraint(NSLayoutConstraint(item: ZheZhaoView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: ZheZhaoView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: ZheZhaoView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: ZheZhaoView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        //房子图片
        addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //信息label
        addConstraint(NSLayoutConstraint(item: textLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: textLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //约束宽度高度,没有参照控件(toItem: nil)的时候,attribute一定要设置为: NSLayoutAttribute.NotAnAttribute
        addConstraint(NSLayoutConstraint(item: textLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 250))
        
        //注册按钮
        addConstraint(NSLayoutConstraint(item: ZhuCeButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: textLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: ZhuCeButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: ZhuCeButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: ZhuCeButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        //登录按钮
        addConstraint(NSLayoutConstraint(item: LoginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: textLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: LoginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: LoginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: ZhuCeButton, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: LoginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: ZhuCeButton, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0))
        
        
        
    }
    
    //懒加载子控件
    ///转轮动画
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    ///界面遮罩
    private lazy var ZheZhaoView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    ///房子图片
    private lazy var houseView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    ///图片下的信息label
    private lazy var textLabel: UILabel = {
        let label = UILabel()
        
        //label属性
        label.text = "关注一些人,看看有什么惊喜!!"
        label.textAlignment = NSTextAlignment.Center
//        label.font = UIFont.systemFontSize(18)
        label.font = UIFont.systemFontOfSize(18)
        label.numberOfLines = 0
        label.sizeToFit()
        return label
    }()
    
    ///注册按钮
    private lazy var ZhuCeButton: UIButton = {
        let button = UIButton()
        
        //设置属性
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState:UIControlState.Normal)
        
        return button
    }()
    
    ///登录按钮
    private lazy var LoginButton: UIButton = {
       
        let button = UIButton()
        
        //设置属性
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        return button
    }()
    

}
